import pandas as pd
from ..utils.format_utils import format_markdown_table

class TableGenerator:
    """从Pandas DataFrame生成Markdown表格"""
    
    @staticmethod
    def generate_from_dataframe(
        df: pd.DataFrame,
        output_path: str = None,
        index: bool = False,
        float_format: str = ".2f"
    ) -> str:
        """
        将DataFrame转换为Markdown表格[1](@ref)
        :param df: 输入数据框
        :param output_path: 输出路径（可选）
        :param index: 是否包含索引列
        :param float_format: 浮点数格式
        """
        markdown_table = df.to_markdown(
            index=index,
            floatfmt=float_format
        )
        
        if output_path:
            from ..generators.file_generator import FileGenerator
            FileGenerator.generate_from_content(
                f"# 数据表格\n\n{markdown_table}",
                output_path
            )
        
        return markdown_table

    @staticmethod
    def generate_filtered_table(
        df: pd.DataFrame,
        filter_condition: str,
        output_path: str = None
    ) -> str:
        """
        生成过滤后的表格（示例：分数>80）[1](@ref)
        """
        filtered_df = df.query(filter_condition)
        return TableGenerator.generate_from_dataframe(
            filtered_df,
            output_path
        )